<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\StockActivity;
use App\Models\StockReward;
use App\Repositories\MemberRepositoryEloquent;

class StockCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'stock:settlement';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '对对乐定时任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(MemberRepositoryEloquent $user)
    {
        parent::__construct();
        $this->user = $user;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //查询开始时间大于开始当前时间并且状态为等待中(0)的活动
        $activities = StockActivity::where('start_time','<',time())->where('end_time','>',time())->get();
        //循环活动
        foreach($activities as $activity){
            //改变活动状态，改变写入订单开盘价
            if($activity->status==0){
                $activity->status = 1;
                $res = $activity->save();
            }
            
            if($activity->status==1){
                $orders = $activity->orders()->where('status',1)->where('open_price','=',0)->get();
                //var_dump($orders);
                $codes = $orders->pluck('stock_code');
                $codes =  $codes->unique();
                $codes = implode(',',$codes->all());
                $res = get_detail_data($codes);
                $datas = collect($res);
                $datas = $datas->keyBy('ticker');
                foreach($orders as $k=>$order){
                    $data = null;
                    if(isset($datas[$order->stock_code])){
                        $data = $datas[$order->stock_code];
                    }
                    if(!$data){
                        continue;
                    }
                    $order->open_price = $data->openPrice;
                    $order->save();
                }

            }
        }

        //查询开始时间大于开始当前时间并且状态为进行中(1)的活动
        $activities = StockActivity::where('end_time','<',time())->where('status','=',1)->get();
        //$activities = StockActivity::where('status','=',1)->get();
        if(!$activities){
            return;
        } 
        //循环活动
        foreach($activities as $activity){
            
            //改变活动状态，改变写入订单结算价
            if($activity->status==1){
                $activity->status = 2;
                $res = $activity->save();
            }

            $orders = $activity->orders()->where('status',1)->where('close_price','=',0)->get();
            //var_dump($orders);
            $codes = $orders->pluck('stock_code');
            $codes =  $codes->unique();
            $codes = implode(',',$codes->all());
            $res = get_detail_data($codes);
            $datas = collect($res);
            $datas = $datas->keyBy('ticker');
            //改变活动状态，改变写入订单结算价
            if(!empty($datas) && $activity->status==1){
                $activity->status = 2;
                $res = $activity->save();
            }
            //dd($orders);
            foreach($orders as $k=>$order){
                $data = null;
                if(isset($datas[$order->stock_code])){
                    $data = $datas[$order->stock_code];
                }
                
                if(!$data){
                    continue;
                }
                //$order->close_price = $data->lastPrice;
                
                $order->close_price = $data->lastPrice;
                if($order->open_price<=0){
                    $order->close_price=0.00;
                }
                //if(!empty($order->close_price) && !empty($order->open_price)){
                if($order->close_price>0 && $order->open_price>0){
                    $yield = sprintf("%.4f",(($order->close_price-$order->open_price)/$order->open_price)*100);
                }else{
                    $yield = 0.00;
                }
                
                $order->yield = $yield;
                $order->save();
            }

        }
    
    }



    
}
